//
//  PopMsg.m
//  LocomotiveGuards
//
//  Created by admin on 15/11/13.
//  Copyright (c) 2015年 REDOOR. All rights reserved.
//

#import "PopMsg.h"
#define Screen_width [UIScreen mainScreen].bounds.size.width
#define Screen_height [UIScreen mainScreen].bounds.size.height

@implementation PopMsg

+(void)showMessage:(NSString *)str Lasttime:(float)seconds{
    float L;
    CGSize textsize=CGSizeMake(0, 25);
    NSDictionary *dic=@{NSFontAttributeName:[UIFont systemFontOfSize:12]};
    CGRect rect=[str boundingRectWithSize:textsize options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:dic context:nil];
    L=rect.size.width+30;
    
    UIWindow *window=[UIApplication sharedApplication].keyWindow;
    UIImageView *showview=[[UIImageView alloc]initWithFrame:CGRectZero];
    showview.backgroundColor=[UIColor clearColor];
    UILabel *label=nil;
    if(L>SCREEN_WIDTH){
        showview.frame=CGRectMake(0, Screen_height*0.8, SCREEN_WIDTH, 40);
        label=[[UILabel alloc]initWithFrame:CGRectMake(10, 0, SCREEN_WIDTH-20, 40)];
        label.numberOfLines=0;
    }
    else{
        
        showview.frame=CGRectMake((Screen_width-L)/2, Screen_height*0.8, L, 30);
        label=[[UILabel alloc]initWithFrame:CGRectMake(10, 0, L-20, 30)];
    }
    
    
    
    showview.backgroundColor=[UIColor blackColor];
    
    [window addSubview:showview];
//    showview.layer.shadowColor=[UIColor purpleColor].CGColor;
    showview.layer.shadowColor = [UIColor blackColor].CGColor;
    showview.layer.shadowOffset = CGSizeMake(0,0);
    showview.layer.shadowOpacity = 0.8;
    label.text=str;
    label.textAlignment=NSTextAlignmentCenter;
    //label.shadowColor=[UIColor whiteColor];
    label.backgroundColor=[UIColor clearColor];
    label.textColor=[UIColor whiteColor];
    label.font=[UIFont systemFontOfSize:12];
    
    [showview addSubview:label];
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        sleep(1);
        dispatch_async(dispatch_get_main_queue(), ^{
            [UIView animateWithDuration:seconds animations:^{
                showview.alpha=0;} completion:^(BOOL finished){
                    [showview removeFromSuperview];
                }];
            
            
            
        });
        
    });
}


@end
